package com.example.bookkeep;

import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

import com.example.bookkeep.Fragment.FinanceFragment;
import com.example.bookkeep.Fragment.HomeFragment;
import com.example.bookkeep.Fragment.ProfileFragment;
import com.google.android.material.bottomnavigation.BottomNavigationView;

public class MainActivity extends AppCompatActivity {

    private Fragment homeFragment = new HomeFragment();
    private Fragment financeFragment = new FinanceFragment();
    private Fragment profileFragment = new ProfileFragment();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        BottomNavigationView bottomNav = findViewById(R.id.bottom_Navigation_View);
        bottomNav.setOnItemSelectedListener(item -> {
            int id = item.getItemId();
            if (id == R.id.nav_home) {
                loadFragment(homeFragment);
            } else if (id == R.id.nav_finance) {
                loadFragment(financeFragment);
            } else if (id == R.id.nav_profile) {
                loadFragment(profileFragment);
            } else {
                return false;
            }
            return true;
        });

        // 默认显示首页
        bottomNav.setSelectedItemId(R.id.nav_home);
    }

    private void loadFragment(Fragment fragment) {
        getSupportFragmentManager().beginTransaction()
            .replace(R.id.nav_host_fragment, fragment)
            .commit();
    }
}
